package com.kangyi.user.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.kangyi.constant.MessageConstant;
import com.kangyi.dto.ApplicationListDTO;
import com.kangyi.dto.NursingHomeApplicationDTO;
import com.kangyi.entity.NursingHomeApplication;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.exception.ToExamineException;
import com.kangyi.result.PageResult;
import com.kangyi.user.mapper.NursingHomeApplicationMapper;
import com.kangyi.user.service.NursingHomeApplicationService;
import com.kangyi.utils.AuthUtil;
import com.kangyi.vo.ApplicationListVO;
import com.kangyi.vo.NursingHomeApplicationDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class NursingHomeApplicationServiceImpl implements NursingHomeApplicationService {
    @Autowired
    private NursingHomeApplicationMapper nursingHomeApplicationMapper;


    @Override
    public void submit(NursingHomeApplicationDTO nursingHomeApplicationDTO) {
        if (nursingHomeApplicationDTO == null) {
            throw new DataNotExistException(MessageConstant.DATA_LACK);
        }
        NursingHomeApplication nursingHomeApplication = new NursingHomeApplication();
        BeanUtil.copyProperties(nursingHomeApplicationDTO,nursingHomeApplication);
        nursingHomeApplication.setStatus(NursingHomeApplication.PENDING_REVIEW);
        nursingHomeApplicationMapper.submit(nursingHomeApplication);
    }

    @Override
    public PageResult listApplication(ApplicationListDTO applicationListDTO) {
        List<ApplicationListVO> result = nursingHomeApplicationMapper.listApplication(applicationListDTO);
        if (CollUtil.isEmpty(result)) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        Long total = nursingHomeApplicationMapper.countTotalByUserId(applicationListDTO.getUserId());
        return new PageResult(total,result);
    }

    @Override
    public void deleteApplication(Integer applicationId) {
        NursingHomeApplication nursingHomeApplication = nursingHomeApplicationMapper.selectById(applicationId);

        if (nursingHomeApplication == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }

        AuthUtil.validateUserPermission(nursingHomeApplication.getUserId());

        if (nursingHomeApplication.getStatus().equals(1)) {
            throw new ToExamineException(MessageConstant.THE_REVIEW_HAS_BEEN_APPROVED_AND_CANNOT_BE_WITHDRAWN);
        }

        nursingHomeApplicationMapper.deleteById(applicationId);
    }

    @Override
    public NursingHomeApplicationDetailVO DetailApplication(Integer applicationId) {
        NursingHomeApplicationDetailVO nursingHomeApplicationDetailVO = nursingHomeApplicationMapper.DetailApplication(applicationId);
        if (nursingHomeApplicationDetailVO == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        Integer userId = nursingHomeApplicationDetailVO.getUserId();
        AuthUtil.validateUserPermission(userId);
        return nursingHomeApplicationDetailVO;
    }
}
